﻿/**
 * @license Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or http://ckeditor.com/license
 */

/**
 * @fileOverview Horizontal Page Break
 */

'use strict';

( function() {
	// Register a plugin named "slidebreak".
	CKEDITOR.plugins.add( 'slidebreak', {
		requires: 'fakeobjects',
		lang: 'en,vi', // %REMOVE_LINE_CORE%
		icons: 'slidebreak,slidebreak-rtl', // %REMOVE_LINE_CORE%
		hidpi: true, // %REMOVE_LINE_CORE%
		onLoad: function() {
			var cssStyles = (
					'background:url(' + CKEDITOR.getUrl( this.path + 'images/slidebreak.gif' ) + ') repeat-x center center;' +
					'clear:both;' +
					'width:100%;' +
					'padding:0;' +
					'height:5px;' +
					'cursor:default;'
				).replace( /;/g, ' !important;' ); // Increase specificity to override other styles, e.g. block outline.

			// Add the style that renders our placeholder.
			CKEDITOR.addCss( 'div.cke_slidebreak{' + cssStyles + '}' );
		},

		init: function( editor ) {
			if ( editor.blockless )
				return;

			// Register the command.
			editor.addCommand( 'slidebreak', CKEDITOR.plugins.slidebreakCmd );

			// Register the toolbar button.
			editor.ui.addButton && editor.ui.addButton( 'Slidebreak', {
				label: editor.lang.slidebreak.toolbar,
				command: 'slidebreak',
				toolbar: 'insert'
			} );

			// Webkit based browsers needs help to select the slide-break.
			CKEDITOR.env.webkit && editor.on( 'contentDom', function() {
				editor.document.on( 'click', function( evt ) {
					var target = evt.data.getTarget();
					if ( target.is( 'div' ) && target.hasClass( 'cke_slidebreak' ) )
						editor.getSelection().selectElement( target );
				} );
			} );
		},

		afterInit: function( editor ) {
			// Register a filter to displaying placeholders after mode change.
			var dataProcessor = editor.dataProcessor,
				dataFilter = dataProcessor && dataProcessor.dataFilter,
				htmlFilter = dataProcessor && dataProcessor.htmlFilter,
				classRegex = /slide-break/i;

			function upcastSlideBreak( element ) {
				CKEDITOR.tools.extend( element.attributes, attributesSet( editor.lang.slidebreak.alt ), true );
			}

			if ( htmlFilter ) {
				htmlFilter.addRules( {
					attributes: {
						'class': function( value, element ) {
							var className = value.replace( 'cke_slidebreak', '' );
							if ( className != value ) {
								var attrs = element.attributes;
								delete attrs[ 'aria-label' ];
								delete attrs.contenteditable;
								delete attrs.title;
							}
							return className;
						}
					}
				}, { applyToAll: true, priority: 5 } );
			}

			if ( dataFilter ) {
				dataFilter.addRules( {
					elements: {
						div: function( element ) {
							if ( element.attributes[ 'data-cke-slidebreak' ] )
								upcastSlideBreak( element );

							// Check for "data form" of the pagebreak. If both element and
							// descendants match, convert them to internal form.
							else if ( classRegex.test( element.attributes.class ) ) {
								upcastSlideBreak( element );
							}
						}
					}
				} );
			}
		}
	} );

	// TODO Much probably there's no need to expose this object as public object.
	CKEDITOR.plugins.slidebreakCmd = {
		exec: function( editor ) {
			// Create read-only element that represents a print break.
			var slidebreak = editor.document.createElement( 'div', {
				attributes: attributesSet( editor.lang.slidebreak.alt )
			} );

			editor.insertElement( slidebreak );
		},
		context: 'div',
		allowedContent: {
			div: {
				'class': '!slide-break'
			}
		},
		requiredContent: 'div{slide-break}'
	};

	// Returns an object representing all the attributes
	// of the "internal form" of the slidebreak element.
	function attributesSet( label ) {
		return {
			'aria-label': label,
			'class': 'cke_slidebreak slide-break',
			contenteditable: 'false',
			'data-cke-display-name': 'slidebreak',
			'data-cke-slidebreak': 1,
			title: label
		};
	}
} )();